﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Collections.Specialized;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.Admin
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            #region
            string appErrMsg = "URL:\r\n" + Request.Url + "\r\n" +
                    "错误描述:\r\n" + Server.GetLastError().Message + "\r\n\r\n" +
                    "详细错误信息:\r\n" + Server.GetLastError().ToString() + "\r\n";
            string appPath = GetApplicationPath();
            string requestUrl = Request.Url.ToString();
            //string WebSrvIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString();

            appErrMsg += GetServerVariable();
            ExceptionMessageManager.Record(new Exception(appErrMsg));

            Exception ex = Server.GetLastError();
            if (!(ex.InnerException is System.IO.FileNotFoundException))
            {
                Server.Transfer(appPath + "/ErrorPages/ErrorPage.aspx");                
            }
            else //404
            {
                Server.Transfer(appPath + "/ErrorPages/404.htm");
            }
            #endregion
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        private string GetServerVariable()
        {
            #region
            string appErrMsg = "";

            int loop1, loop2;
            NameValueCollection coll;

            // Load ServerVariable collection into NameValueCollection object.
            coll = Request.ServerVariables;
            // Get names of all keys into a string array. 
            String[] arr1 = coll.AllKeys;
            for (loop1 = 0; loop1 < arr1.Length; loop1++)
            {
                appErrMsg += ("Key: " + arr1[loop1] + "\r\n");
                String[] arr2 = coll.GetValues(arr1[loop1]);
                for (loop2 = 0; loop2 < arr2.Length; loop2++)
                {
                    appErrMsg += ("Value " + loop2 + ": " + arr2[loop2] + "\r\n");
                }
            }
            return appErrMsg;
            #endregion
        }

        public static string GetApplicationPath()
        {
            string path = HttpContext.Current.Request.ApplicationPath;
            if (path.EndsWith("/") == false)
            {
                path += "/";
            }
            return path;
        }
    }
}